package com.dm.cloud.images.controller;

import com.dm.cloud.core.mode.ResultModel;
import com.dm.cloud.images.mode.ContainerCreateMode;
import com.dm.cloud.images.mode.ContainerDetail;
import com.dm.cloud.images.service.ContainerService;
import com.dm.cloud.oauthlib.service.anon.HasLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/apis/v1/container")
@Tag(name = "容器管理")
@HasLogin
public class ContainerController {

    @Autowired
    private ContainerService containerService;

    @GetMapping("list")
    @Operation(summary = "容器列表查询")
    public ResultModel<List<ContainerDetail>> list(
            @Parameter(description = "容器名称",in= ParameterIn.QUERY) String name,
            @Parameter(description = "节点ID",in = ParameterIn.QUERY) String nodeId
    ){
        return ResultModel.success(containerService.list(name,nodeId));
    }

    @PutMapping("upApp")
    @Operation(summary = "修改容器关联的应用")
    public ResultModel<ContainerDetail> upApp(
            @Parameter(description = "容器id",in= ParameterIn.QUERY) String id,
            @Parameter(description = "节点ID",in = ParameterIn.QUERY) String nodeId,
            @Parameter(description = "应用Code",in = ParameterIn.QUERY) String appCode
    ){
        return ResultModel.success(containerService.upApp(id,nodeId,appCode));
    }

    @GetMapping("detail")
    @Operation(summary = "查询容器详情")
    public ResultModel<ContainerDetail> detail(
            @Parameter(description = "容器名称/id",in= ParameterIn.QUERY) String id,
            @Parameter(description = "节点ID",in = ParameterIn.QUERY) String nodeId
    ){
        return ResultModel.success(containerService.detail(id,nodeId));
    }

    @PostMapping("create")
    @Operation(summary = "创建容器")
    public ResultModel<ContainerDetail> create(@RequestBody ContainerCreateMode containerCreateMode){
        return ResultModel.success(containerService.create(containerCreateMode));
    }

    @PutMapping("start")
    @Operation(summary = "启动容器（停止状态下可执行）")
    public ResultModel<ContainerDetail> start(
            @Parameter(description = "节点",in= ParameterIn.QUERY)String nodeId,
            @Parameter(description = "容器id",in= ParameterIn.QUERY)String id){
        return ResultModel.success(containerService.start(nodeId,id));
    }

    @PutMapping("stop")
    @Operation(summary = "停止容器（启动状态下可执行）")
    public ResultModel<ContainerDetail> stop(
            @Parameter(description = "节点",in= ParameterIn.QUERY)String nodeId,
            @Parameter(description = "容器id",in= ParameterIn.QUERY)String id){
        return ResultModel.success(containerService.stop(nodeId,id));
    }

    @PutMapping("restart")
    @Operation(summary = "重新启动容器（启动状态下执行）")
    public ResultModel<ContainerDetail> restart(
            @Parameter(description = "节点",in= ParameterIn.QUERY)String nodeId,
            @Parameter(description = "容器id",in= ParameterIn.QUERY)String id){
        return ResultModel.success(containerService.restart(nodeId,id));
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除容器")
    public ResultModel<Boolean> delete(
            @Parameter(description = "节点",in= ParameterIn.QUERY)String nodeId,
            @Parameter(description = "容器id",in= ParameterIn.QUERY)String id,
            @Parameter(description = "是否删除关联数据卷,默认false",in=ParameterIn.QUERY) Boolean delV){
        return ResultModel.success(containerService.delete(nodeId,id,delV));
    }

}
